package com.dirtylily.myblog.service;

import com.dirtylily.myblog.dao.TagDao;
import com.dirtylily.myblog.dao.TypeDao;
import com.dirtylily.myblog.pojo.Tag;
import com.dirtylily.myblog.pojo.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TagService {
    @Autowired
    TagDao tagDao ;

    public Tag getTag(Long id){

        try {

            Tag tag = tagDao.getTag(id);
            return tag;

        }
        catch (Exception e){
            System.out.println("标签查找失败");
        }
        return null;
    }

    public int updateTag(Tag tag){

        int i = tagDao.updateTag(tag);

        return i;
    }

    public List<Tag> getAllTag() {
        return tagDao.getAllTag();
    }

    public int saveTag(Tag tag){
        int i = tagDao.saveTag(tag);
        return i;
    }


    public int delTag(Long id){
        int i = tagDao.delTag(id);
        return i;
    }

    public Tag getTagByName(String name){
        Tag tagByName = tagDao.getTagByName(name);
        return tagByName;
    }
    public List<Tag> convertToList(String tagIDs){
        ArrayList<Tag> tags = new ArrayList<>();
        if(tagIDs!=null){
            if(!"".equals(tagIDs)){
                String[] split = tagIDs.split(",");
                for (String s : split) {
                    tags.add(this.getTag(Long.parseLong(s)));
                }

            }
        }

        return tags;
    }

    public List<Tag> getTopTag(Integer num){
        Integer number = num;
        List<Tag> allTag = tagDao.getAllTag();
        if(allTag.size()<=num){
            return allTag;
        }
        List<Tag> lateTags = new ArrayList<>();
        for(int i = allTag.size()-1; number>0;i--,number--){
            lateTags.add(allTag.get(i));

        }
        return lateTags;
    }
}

